package com.example.demo.Service;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.example.demo.Entity.Log;
import com.example.demo.Entity.Message;
import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Repository.IHRRepository;
import com.example.demo.Repository.ILogRepository;
import com.example.demo.Repository.IMessageRepository;

/**
 * @version: V0.3
 * @author: 吴嘉鑫
 * @className: MessgaeService
 * @packageName: com.example.demo.Service
 * @description: 这是实现广场上传发布信息的服务类
 * @data: 2021-07-20
 **/
@Service
public class MessageService {
	@Autowired
	private IMessageRepository mesRepository;
	@Autowired
	private ILogRepository logRepository;
	@Autowired
	private IHRRepository hrRepository;
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: sendHrMes
	* @description: HR发布招聘信息
	* @param:  String username,Integer salary,String position,String text
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> sendHrMes(String username,Integer salary,String position,String text){
		Message mes = new Message();
		mes.setUserName(hrRepository.findCompanyNamebyUsername(username));
		mes.setUserType(true);
		mes.setSalary(salary);
		mes.setPosition(position);
		mes.setText(text);
		mesRepository.save(mes);
		return APIResponse.success("successsend",null);
	}
	
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: sendEmpMes
	* @description:emp发布求职信息
	* @param:  String username,Integer salary,String position,String text
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> sendEmpMes(String username,Integer salary,String position,String text){
		Message mes = new Message();
		mes.setUserName(username);
		mes.setUserType(false);
		mes.setSalary(salary);
		mes.setPosition(position);
		mes.setText(text);
		mesRepository.save(mes);
		return APIResponse.success("successsend",null);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getLatestHr
	* @description: 返回最新招聘信息
	* @param:
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getLatestHr(){
		List<Message> list = mesRepository.findByuserType(true);
		Collections.sort(list);
		if (list.size() > 10) {
            //     List newList = list.subList(0, 4);//取前四条数据  
            //判断list长度 只保留最后10位
             list.subList(0,list.size()-10).clear();
        }
		return APIResponse.success("successget",list);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getLatestHr
	* @description: 返回最新求职信息
	* @param: 
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getLatestEmp(){
		List<Message> list = mesRepository.findByuserType(false);
		if (list.size() > 10) {
            //     List newList = list.subList(0, 4);//取前四条数据  
            //判断list长度 只保留最后10位
             list.subList(0,list.size()-10).clear();
        }
		return APIResponse.success("successget",list);
	}
	
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getAllHr
	* @description: 返回所有招聘信息
	* @param: 
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getAllHr(){
        List<Message> list = mesRepository.findByuserType(true);
		return APIResponse.success("successget",list);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getAllEmp
	* @description: 返回所有求职信息
	* @param: Integer page
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getAllEmp(){
        List<Message> list = mesRepository.findByuserType(false);
		return APIResponse.success("successget",list);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: empGetMyMessage
	* @description: 返回员工自己的求职信息
	* @param: String username
	* @return: List<Message>
	* @throws: 
	*/
	public List<Message> empGetMyMessage(String username){
		return mesRepository.findByUserName(username);
	}
	
	
	
	/**
	* @author:郑皓杰
	* @methodsName: hrGetMyMessage
	* @description: 返回公司自己的招聘信息
	* @param: String username
	* @return: List<Message>
	* @throws: 
	*/
	public List<Message> hrGetMyMessage(String username){
		return mesRepository.findByUserName(hrRepository.findCompanyNamebyUsername(username));
	}
		
	/**
	* @author:郑皓杰
	* @methodsName: modifyMessage
	* @description: 修改自己的某一条信息
	* @param: Integer mesid,Integer salary,String position,String text
	* @return: APIResponse<?>
	* @throws: 
	*/
	public APIResponse<?> modifyMessage(Integer mesid,Integer salary,String position,String text){
		Message mes=mesRepository.findByMesID(mesid);
		mes.setSalary(salary);
		mes.setPosition(position);
		mes.setText(text);
		mesRepository.save(mes);
		return APIResponse.success("success modify",null);
	}
	
	
	/**
	* @author:郑皓杰
	* @methodsName: deleteMessage
	* @description: 删除自己的某一条信息
	* @param: Integer mesid
	* @return: APIResponse<?>
	* @throws: 
	*/
	public APIResponse<?> deleteMessage(Integer mesid){
		mesRepository.delete(mesRepository.findByMesID(mesid));
		return APIResponse.success("success delete",null);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: getWordData
	* @description: 返回所有讯息
	* @param: 
	* @return: List<Log>
	* @throws: 
	*/
	public List<Log> getWordData(){
		return logRepository.findAll();
	}
	
	
	
}








